package com.luping.xiao.xiao.service.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luping.xiao.xiao.dao.OpExceptionDao;
import com.luping.xiao.xiao.domain.OpException;
import com.luping.xiao.xiao.service.OpExceptionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class OpExceptionServiceImpl extends ServiceImpl<OpExceptionDao, OpException> implements OpExceptionService {
    protected static final Logger log = LoggerFactory.getLogger(OpExceptionServiceImpl.class);

    @Autowired
    private OpExceptionDao opExceptionDao;

    @Override
    public List<OpException> queryList(OpException flow) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Integer num = opExceptionDao.queryListCount(flow);
        log.info("查询数据：" + num);
        if (num != null && num > 0) {
            int insertNum = 0;
            int limitStart = 3000;
            for (int i = 0; i < 5000; i++) {
                if (i == 0) {
                    flow.setLimitStart(0);
                } else {
                    flow.setLimitStart(i * limitStart);
                }
                flow.setLimitEnd(3000);
                List<OpException> list = opExceptionDao.queryList(flow);
                log.info("查询：" + i + "查询数据从" + flow.getLimitStart() + "查询3000条，数据：" + list.size());
                if (list != null && list.size() > 0) {
                    for (OpException f : list) {
                        if (f.getOccurtime() != null) {
                            String occtime = format.format(f.getOccurtime());
                            occtime = flow.getOct() + occtime.substring(10);
                            f.setOccurtime(format.parse(occtime));
                        }
                        if (f.getServertime() != null) {
                            String sertime = format.format(f.getServertime());
                            sertime = flow.getSert() + sertime.substring(10);
                            f.setServertime(format.parse(sertime));
                        }

                    }
                    insertBa(list);
                    insertNum = insertNum + list.size();
                }
                log.info("插入数据：" + insertNum);
                if (insertNum >= num) {
                    log.info("insertBa:" + i);
                    break;
                }
            }
        }
        return null;
    }


    public void insertBa(List<OpException> list) {
        int insertLength = list.size();
        int i = 0;
        while (insertLength > 1000) {
            saveBatch(list.subList(i, i + 1000));
            i = i + 1000;
            insertLength = insertLength - 1000;
        }
        if (insertLength > 0) {
            saveBatch(list.subList(i, i + insertLength));
        }
        log.info("insertBa");
    }

    public void saveBatch(List<OpException> list) {
        try {
            opExceptionDao.saveBatch(list);
        } catch (Exception e) {
            for (OpException l : list) {
                log.info("失败数据：" + l.toString());
            }
            e.printStackTrace();
        }

    }
}
